package com.zxy.supplier_system.server.service

import com.zxy.supplier_system.server.controller.GoodsCategoryController
import com.zxy.supplier_system.server.entity.GoodsCategory
import com.zxy.supplier_system.server.repository.GoodsCategoryRepository
import com.zxy.supplier_system.server.repository.GoodsRepository
import com.zxy.supplier_system.server.repository.SystemServiceRepository
import com.zxy.supplier_system.server.utils.IntIdAndName
import com.zxy.supplier_system.server.utils.orElse404
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class GoodsCategoryService(
    private val goodsCategoryRepository: GoodsCategoryRepository,
    private val goodsRepository: GoodsRepository,
    private val systemServiceService: SystemServiceService,
    private val employeeOperateRecordService: EmployeeOperateRecordService,
) {


    @Transactional(readOnly = true)
    fun queryGoodsCategories(
        pageable: Pageable,
        queryGoodsCategoriesRequest: GoodsCategoryController.QueryGoodsCategoriesRequest
    ): Page<GoodsCategoryController.QueryGoodsCategoriesResponse> {
        return this.goodsCategoryRepository.findAll(
            GoodsCategoryRepository.Specifications.fromQueryGoodsCategories(
                queryGoodsCategoriesRequest
            ).and(SystemServiceRepository.Specifications.systemServiceEquals({root ->  root},systemServiceService.getCurrentSystemService())), pageable
        ).map {
            GoodsCategoryController.QueryGoodsCategoriesResponse(
                it.name,
                it.id,
                this.goodsRepository.countAllByCategory(it)
            )
        }
    }

    @Transactional
    fun updateGoodsCategory(id: Int, updateGoodsCategoryRequest: GoodsCategoryController.UpdateGoodsCategoryRequest) {
        val data = this.goodsCategoryRepository.findByIdOrNull(id).orElse404()
        data.name = updateGoodsCategoryRequest.name
        this.goodsCategoryRepository.save(data)
        this.employeeOperateRecordService.updateGoodsCategory(data)
    }

    @Transactional
    fun createGoodsCategory(updateGoodsCategoryRequest: GoodsCategoryController.UpdateGoodsCategoryRequest) {
        val goodsCategory = this.goodsCategoryRepository.save(GoodsCategory().apply {
            name = updateGoodsCategoryRequest.name
            systemService = systemServiceService.getCurrentSystemService()
        })
        this.employeeOperateRecordService.createGoodsCategory(goodsCategory)
    }

    @Transactional
    fun deleteGoodsCategory(id: Int) {
        this.goodsCategoryRepository.deleteById(id)
    }

    fun getAllGoodsCategories(): List<IntIdAndName> {
        return this.goodsCategoryRepository.findAllBySystemService(systemServiceService.getCurrentSystemService()).map {
            IntIdAndName(it.id, it.name)
        }
    }


}
